import re
from PyQt6.QtCore import Qt, pyqtSignal, QUrl, QEvent
from PyQt6.QtGui import QDesktopServices, QPainter, QPen, QColor
from PyQt6.QtWidgets import QWidget, QLabel, QVBoxLayout, QHBoxLayout, QFrame

from qfluentwidgets import (
    ScrollArea,
    PushButton,
    ToolButton,
    FluentIcon,
    isDarkTheme,
    IconWidget,
    Theme,
    ToolTipFilter,
    TitleLabel,
    CaptionLabel,
    StrongBodyLabel,
    BodyLabel,
    toggleTheme,
)
from .common.config import cfg, FEEDBACK_URL, HELP_URL, EXAMPLE_URL
from .common.icon import Icon
from .common.style_sheet import StyleSheet
from .common.signal_bus import signalBus


class GalleryInterface(ScrollArea):
    """Gallery interface"""

    def __init__(self, title: str, subtitle: str, parent=None):
        """
        Parameters
        ----------
        title: str
            The title of gallery

        subtitle: str
            The subtitle of gallery

        parent: QWidget
            parent widget
        """
        super().__init__(parent=parent)
        self.view = QWidget(self)
        self.vBoxLayout = QVBoxLayout(self.view)

        self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
        self.setWidget(self.view)
        self.setWidgetResizable(True)

        self.vBoxLayout.setSpacing(30)
        self.vBoxLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.vBoxLayout.setContentsMargins(36, 20, 36, 36)

        self.view.setObjectName("view")
        StyleSheet.GALLERY_INTERFACE.apply(self)

        self.initParam = {
            "changeKey": "",
        }

    def addViewCard(self, title, widget, stretch=0):
        card = BaseViewCard(widget, stretch, self.view)
        self.vBoxLayout.addWidget(card, 1, Qt.AlignmentFlag.AlignTop)
        return card

    def scrollToCard(self, index: int):
        """scroll to example card"""
        w = self.vBoxLayout.itemAt(index).widget()
        self.verticalScrollBar().setValue(w.y())
        self.doUpdatePage()

    def setExchangeData(self, data):
        self.initParam["changeKey"] = data

    def getScrollToCardData(self):
        return self.initParam["changeKey"]

    def doInitAndUpdate(self):
        AntLogger.info(f"执行方法")

    def doUpdatePage(self):
        AntLogger.info(f"执行方法")


class BaseViewCard(QWidget):
    """Example card"""

    def __init__(self, widget: QWidget, stretch=0, parent=None):
        super().__init__(parent=parent)
        self.widget = widget
        self.stretch = stretch

        self.card = QFrame(self)
        self.sourceWidget = QFrame(self.card)
        self.linkIcon = IconWidget(FluentIcon.LINK, self.sourceWidget)

        self.vBoxLayout = QVBoxLayout(self)
        self.cardLayout = QVBoxLayout(self.card)
        self.topLayout = QHBoxLayout()

        self.__initWidget()

    def __initWidget(self):
        self.linkIcon.setFixedSize(16, 16)
        self.__initLayout()

        self.sourceWidget.setCursor(Qt.CursorShape.PointingHandCursor)
        self.sourceWidget.installEventFilter(self)

        self.card.setObjectName("card")
        self.sourceWidget.setObjectName("sourceWidget")

    def __initLayout(self):
        self.vBoxLayout.setSizeConstraint(QVBoxLayout.SizeConstraint.SetMaximumSize)
        self.cardLayout.setSizeConstraint(QVBoxLayout.SizeConstraint.SetMaximumSize)
        self.topLayout.setSizeConstraint(QHBoxLayout.SizeConstraint.SetMaximumSize)

        self.vBoxLayout.setSpacing(12)
        self.vBoxLayout.setContentsMargins(0, 0, 0, 0)
        self.topLayout.setContentsMargins(12, 0, 12, 0)
        self.cardLayout.setContentsMargins(0, 0, 0, 0)

        self.vBoxLayout.addWidget(self.card, 0, Qt.AlignmentFlag.AlignTop)
        self.vBoxLayout.setAlignment(Qt.AlignmentFlag.AlignTop)

        self.cardLayout.setSpacing(0)
        self.cardLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.cardLayout.addLayout(self.topLayout, 0)
        self.cardLayout.addWidget(self.sourceWidget, 0, Qt.AlignmentFlag.AlignBottom)

        self.widget.setParent(self.card)
        self.topLayout.addWidget(self.widget)
        if self.stretch == 0:
            self.topLayout.addStretch(1)

        self.widget.show()
